package com.example.datn.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.datn.MainActivity;
import com.example.datn.R;
import com.example.datn.favorite.MyDataHelper;

public class WebTinTucActivity extends Activity {
	
	private String link=null;
	
	private String title=null;
	
	private MyDataHelper dataHelper;

	private WebView webview;
	private static final String TAG = "Main";
    private ProgressDialog progressBar;
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.web_view);
		
		Bundle bundle =getIntent().getExtras();

		 link=bundle.getString("LINK");
		 title=bundle.getString("TITLE");
		 dataHelper = new MyDataHelper(WebTinTucActivity.this);
		
		
		webview=(WebView)findViewById(R.id.webView1);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        progressBar = ProgressDialog.show(WebTinTucActivity.this, "Đang tải dữ liệu", "Loading...");
		
	
	webview=(WebView)findViewById(R.id.webView1);
	webview.getSettings().setSupportZoom(true);
	webview.setInitialScale(1);
	webview.getSettings().setLoadWithOverviewMode(true);
	webview.getSettings().setUseWideViewPort(true);
	webview.setScrollbarFadingEnabled(false);
	webview.getSettings().setJavaScriptEnabled(true);
	webview.loadUrl(link);
	webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            Log.i(TAG, "Finished loading URL: " + url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(TAG, "Error: " + description);
            Toast.makeText(WebTinTucActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            alertDialog.setTitle("Error");
            alertDialog.setMessage(description+". \nXem lại kết nối internet của bạn");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
            alertDialog.show();
        }
    });
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		 MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.favorite_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.favorite) {
			
			favorite();
			return true;
		}else if(id==R.id.share){
			share();
		}
		return super.onOptionsItemSelected(item);
	}


	private void share() {
		
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT,link);
		startActivity(Intent.createChooser(intent, "Share"));
	}


	private void favorite() {
		Toast.makeText(WebTinTucActivity.this, "Đã lưu vào mục yêu thích", Toast.LENGTH_LONG).show();
		System.out.println("hiiiiiiii:   "+title);
		String title2=title.replace("<b>","");
		System.out.println("heeeee   "+title2);
		String title3=title2.replace("</b>","");
		System.out.println("huuuuuuu  "+title3);
		dataHelper.insertDB(title3, link);
	}

}
